<?php

class Upload extends CI_Controller {
 
 function __construct()
 {
  parent::__construct();
  $this->load->helper(array('form', 'url'));
 }
 
 function index()
 { 
  $this->load->view('upload', array('error' => ' ' ));
 }

 function do_upload()
 {
  $config['upload_path'] = $this->config->item('upload_path');
  $config['allowed_types'] = $this->config->item('allowed_types');
//  $config['max_size'] = '100';
//  $config['max_width']  = '1024';
//  $config['max_height']  = '768';

  
  $this->load->library('upload', $config);
  
  $field_name = "100003";
  
  $this->load->library('tools');
  
  
 
  if ( ! $this->upload->do_upload('fileUpload'))
  {
   $error = array('error' => $this->upload->display_errors());
   
   $this->load->view('upload', $error);
//   echo -1
  } 
  else
  {
   $user = $this->input->post('userID');
   $seqence = $this->input->post('random'); 
   $md5 = $this->input->post('MD5'); 
   $short_path = $user.'/'.$seqence;
   $path =$this->config->item('target_path').$short_path;  
    
   $this->load->model('Resource_Model');   
   $resource = array("create_date"=>date("Y-m-d H:i:s"), "resource_type"=>1, "uri"=>$short_path,"version"=>0,"block"=>0,"md5"=>$md5);   
   $this->Resource_Model->insertResource($resource);
   

   
//   echo "path is ".$path;
//   echo "<br>";
//   if(!$this->checkFolderExist($path)){
	if(!$this->tools->checkFolderExist($path)){
   		$this->tools->createFolder($path);
   }
   $fileInfo=$this->upload->data();   
   $data = array('upload_data' =>$fileInfo );   
   $this->tools->resize($fileInfo['file_name'],$path.'/'.$field_name.$fileInfo['file_ext']);
   $this->load->view('upload_success', $data);
//   echo -1; 
   
   }
 }

}
?>
